import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/index.vue'
import { islogin } from '../utils/index'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/home'
},
{
  path: '/home',
  name: '首页',
  component: Home
},
{
  path: '/cart',
  name: '购物车',
  meta: {
    needlogin: true
  },
  component: () => import('../views/Cart/index.vue')
},
{
  path: '/cate',
  name: '分类',
  component: () => import('../views/Cate/index.vue')
},
{
  path: '/search',
  name: '搜索',
  component: () => import('../views/Search/index.vue')
},
{
  path: '/details',
  name: '详情',
  component: () => import('../views/Details/index.vue')
},
{
  path: '/itemlists',
  name: '商品列表',
  component: () => import('../views/ItemLists/index.vue')
},
{
  path: '/romance',
  name: '浪漫倾城',
  meta: {
    needlogin: true
  },
  component: () => import('../views/Romance/index.vue')
},
{
  path: '/order',
  name: '订单页',
  meta: {
    needlogin: true
  },
  component: () => import('../views/Order/index.vue')
},
{
  path: '/addlinkman',
  name: '添加联系人',
  meta: {
    needlogin: true
  },
  component: () => import('../views/AddLinkMan/index.vue')
},
{
  path: '/linkman',
  name: '联系人地址',
  meta: {
    needlogin: true
  },
  component: () => import('../views/LinkMan/index.vue')
},
{
  path: '/linkmanupdate',
  name: '编辑联系人',
  meta: {
    needlogin: true
  },
  component: () => import('../views/LinkManUpdate/index.vue')
},
{
  path: '/user',
  name: 'Quiet',
  component: () => import('../views/User/index.vue')
},
{
  path: '/login',
  name: '登录',
  component: () => import('../views/Login/index.vue')
},
{
  path: '/register',
  name: '注册',
  component: () => import('../views/Register/index.vue')
},
{
  path: '/notfound',
  name: '404',
  component: () => import('../views/NotFound/index.vue')
}
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.needlogin) {
    if (islogin()) {
      next()
    } else {
      next({
        path: '/login',
        query: {
          from: to.path
        }
      })
    }
  } else {
    next()
  }
})

export default router
